#cubeb = -l:libcubeb.a -lavrt -luuid
cubeb = -lcubeb -lavrt -luuid -lksuser
#portaudio = -l:libportaudio.a
portaudio = -lportaudio
#freetype = -l:libfreetype.a -l:libbrotlidec-static.a -l:libbrotlicommon-static.a -lz -l:libbz2.a
cubeblinux = -lcubeb
freetype = -lfreetype -lharfbuzz
kissfft = -IC:/Stuff/vsync/j/kissfft-master
eigen = -IC:/Users/adm/Downloads/eigen/
linuxflags = -ob -std=c++20 -lGL -lglfw -I/mnt/hgfs/j $(cubeblinux) -lpthread -I/usr/include/freetype2/ $$(llvm-config --ldflags --libs) -labsl_hash -labsl_city -labsl_raw_hash_set $(freetype)
windowsaudio = $(cubeb) -IC:/Users/adm/Downloads/cubeb/cubeb/include -LC:/Users/adm/Downloads/cubeb/cubeb-build/
staticwindows = $(windowsaudio) -l:libglfw3.a -lole32 -lgdi32 -lwinmm -l:libboost_random-mt.a -static-libstdc++ -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread -Wl,-Bdynamic -l:libabsl_raw_hash_set.a -ll:libabsl_hash.a $$(llvm-config --ldflags --libs)
dynamicwindows = $(windowsaudio) -lglfw3 -lole32 -lgdi32 -lwinmm -lboost_random-mt -labsl_hash -labsl_city -labsl_low_level_hash -labsl_raw_hash_set $$(llvm-config --ldflags --libs)
#WARNING: -lLLVM-13 needs to be updated regularly!
#can use $(pkg-config --libs absl_flat_hash_map) instead of the various absl libraries. or use this in a console to find the right libraries. absl's pkg-config is over-promiscuous and links way too many things, even causing compilation to fail
#however, for makefiles, need to use $$() instead of $()
#winmm needed for timeBeginPeriod
#-lddraw -ldxguid for the ddraw vblank.
windowsflags = -ob -std=c++20 -IC:/Stuff/vsync/j $(dynamicwindows) $(freetype)
switchwindowsflags = -std=c++20 -IC:/Stuff/vsync/j $(dynamicwindows) $(freetype)
optimize = -O2 -DNDEBUG -fno-exceptions -fno-rtti -s -march=native
optimizeprofiler = -O2 -DNDEBUG -fno-exceptions -fno-rtti -march=native -gstabs+ -fno-omit-frame-pointers
optimizeasm = -O2 -DNDEBUG -fno-exceptions -fno-rtti -march=native
optimizedlib = -O2 -DNDEBUG -fno-rtti -march=native
#dlib requires exceptions to work
#Nov 2020: Very Sleepy barely works with mingw. gdwarf-2 fails. all function names are just 64-bit integers. no source available. gstabs+ works but no source lines
#it doesn't detect the callback thread, and completely misses the audio cost.

#future: switch back from -O2 to -Ofast -flto when we release

#https://stackoverflow.com/questions/15606993/g-optimization-flags-fuse-linker-plugin-vs-fwhole-program
#fwhole-program may be useless

#Brotli causes a giant text block containing "aria-label" in the executable
#"This generalized iteration also improved the compression ratio by using a pre-defined dictionary of frequently used words and phrases"

#static linking gives 0.3% performance boost.
#it makes distribution smaller and easier
#it makes servicing harder, but who is going to replace dlls with up-to-date ones?
#PGO might give greater boosts
#https://stackoverflow.com/questions/140061/when-to-use-dynamic-vs-static-libraries
#https://stackoverflow.com/questions/1993390/static-linking-vs-dynamic-linking

#-ffunction-sections -fdata-sections -Wl,--gc-sections actually increases file size in MinGW! but not for music_test. seems like it varies.
#now it's back to reducing file size.

#https://github.com/msys2/MINGW-packages/issues/813
#for when inflateReset2 and libpng16-16.dll errors come up.
#copy zlib1.dll to the folder
#or when gzdirect comes up. might be related to abseil

#sudo apt install libeigen3-dev libmpfrc++-dev libdlib-dev


#inside msys2:
#./vcpkg install dlib --triplet=x64-mingw-dynamic
#didn't work. only --triplet=x64-windows works, for Visual Studio

#./vcpkg install eigen3 --triplet=x64-mingw-dynamic
#worked

#./vcpkg install folly --triplet=x64-mingw-dynamic
#didn't work. first installed VS Studio. then Windows 10 SDK, otherwise it didn't build. then ran into "WinHttpSendRequest() failed: 12175", when trying to download openSSL, even though I could download it through firefox.

#pacman -S mingw-w64-x86_64-lapack mingw-w64-x86_64-openblas
#pacman -S mingw-w64-x86_64-glfw mingw-w64-x86_64-kiss_fft
#	there's a ucrt version for all packages. according to https://stackoverflow.com/questions/67848972/differences-between-msvcrt-ucrt-and-vcruntime-libraries, UCRT is the modern version of the C library, better than the regular one. the package name is mingw-w64-ucrt-x86_64-glfw. however, it tried to install more libraries, so not sure about that.
# then I'd need to use /ucrt64 instead of /mingw64

#fno-exceptions removes 4KB

#boost is annoying on linux. sudo apt install libboost-dev -> lacking some features such as threading, locks. linker errors
#sudo apt install libboost-all-dev -> installs a truckload of things, including a gcc version one-obsolete


r: unity.cpp
	g++ unity.cpp $(linuxflags) $(optimize) $(a)
	./b
#clang works too, but not after loading emsdk environment.

#dlib: unity.cpp
#	g++ reverb_matrix.cpp -ob -std=c++20 -I/mnt/hgfs/j -I/mnt/hgfs/j/kissfft-master /mnt/hgfs/j/kissfft-master/kiss_fft.c /mnt/hgfs/j/kissfft-master/tools/kiss_fftr.c -I/usr/include/eigen3/ $(optimizedlib) -ldlib -Wfatal-errors $(a)
#	./b

gdb: unity.cpp
	g++ unity.cpp $(linuxflags) -I/mnt/hgfs/j -O1 $(a)
	#gdb -q -ex run b
	lldb -o run b
	#./b
#-Og optimizes out all the interesting things I need to see
#lldb -o run b
#lldb seems to fail at printing anything. trying to call functions or print containers gives the wrong results. but gdb seems to have this problem too. maybe g++ is the culprit.
#also, no arrow keys because of lack of libedit.
#arrow keys work now, 2022
#lldb has no tab completion
# -lboost_system -lboost_thread-mt might be necessary on Linux, if we use boost locks
# -g makes it take fucking forever. more than an hour, sometimes. I think it's disk access
# and since we can't use -g, no reason to use O0.

benchmusic: music_benchmark.cpp
	g++ music_benchmark.cpp $(windowsflags) $(optimize)


fourier: fft_reverb.cpp
	g++ fft_reverb.cpp -IC:/Users/adm/Downloads/kissfft-master $(windowsflags) $(optimize)

#pip install numpy scikit-optimize
findconstants: unity.cpp wrap_vsync.py
	g++ -c -fPIC vsync.cpp -o vsync.o -std=c++1z -fno-exceptions -fno-rtti -I/mnt/hgfs/j -ffunction-sections -fdata-sections -Wl,--gc-sections -Ofast -DNDEBUG -fvisibility=hidden -DVSYNC_BENCH -DFIND_CONSTANTS
	g++ -shared -Wl,-soname,libvsync.so -o libvsync.so vsync.o -fvisibility=hidden -Ofast
	rm vsync.o
	python wrap_vsync.py


#make a=-fsanitize=address
#a=-fsanitize=undefined
#a=-fsanitize=thread
#a=-fsanitize=memory -fPIE -pie
#the a= lets us pass in extra stuff
#a=-fsanitize=leak is useless, it's part of address sanitizer

analyze: unity.cpp
	clang++ --analyze unity.cpp -std=c++17 -ob -I/mnt/hgfs/j

release: unity.cpp
	g++ unity.cpp $(linuxflags) $(optimize) -s

pg: unity.cpp
	g++ unity.cpp $(linuxflags) $(optimize) -fprofile-generate -fprofile-dir=etc
pu: unity.cpp
	g++ unity.cpp $(linuxflags) $(optimize) -fprofile-use -fprofile-dir=etc

#size -Os is not useful, performs worse than O1
#to see filesizes of functions/objects:
nm:
	nm -CS --size-sort b

asm: music_benchmark.cpp
	g++ music_benchmark.cpp $(windowsflags) $(optimizeasm) -fverbose-asm
#use gdb, and then run "layout asm" inside it after browsing to the right frame. use error() to get a breakpoint

asml: music/music_test.cpp
	g++ music/music_test.cpp $(linuxflags) $(optimizeasm) -fverbose-asm $(a)

#this is for mingw-w64 on Windows. msys2
#I used "pacman -S make mingw-w64-x86_64-gcc gdb mingw-w64-x86_64-clang"
rm:
	g++ unity.cpp $(kissfft) $(windowsflags) $(optimize)
# C:/Stuff/j/kissfft-master/kiss_fft.c C:/Stuff/j/kissfft-master/tools/kiss_fftr.c
#kiss_fft.c can't be included in a file which includes wingdi.h, there's a #define conflict
#kiss_fft.c tools/kiss_fftr.c can be safely removed if I'm not trying to do Fourier stuff

rmd:
	g++ unity.cpp $(kissfft) $(windowsflags) -fno-rtti -fno-exceptions -g -Wall -march=native -O0
# -fno-omit-frame-pointer useful?
# absl's hash map refuses to compile because -labsl_hash is missing, unless O>1 and flto are chosen. I don't know why.

science_windows:
	g++ reverb_matrix.cpp $(kissfft) $(eigen) $(windowsflags) $(optimize) -IC:/Users/adm/Downloads/optim-master/header_only_version
#g++ reverb_matrix.cpp $(kissfft) $(eigen) $(switchwindowsflags) $(optimize) -IC:/Users/adm/Downloads/optim-master/header_only_version -DSWITCH=0 -o0
#g++ reverb_matrix.cpp $(kissfft) $(eigen) $(switchwindowsflags) $(optimize) -IC:/Users/adm/Downloads/optim-master/header_only_version -DSWITCH=1 -o1
#g++ reverb_matrix.cpp $(kissfft) $(eigen) $(switchwindowsflags) $(optimize) -IC:/Users/adm/Downloads/optim-master/header_only_version -DSWITCH=2 -o2
#g++ reverb_matrix.cpp $(kissfft) $(eigen) $(switchwindowsflags) $(optimize) -IC:/Users/adm/Downloads/optim-master/header_only_version -DSWITCH=3 -o3

sdl:
	#for dynamic linking:
	g++ sdl.cpp -ob -std=c++20 -IC:/Stuff/vsync/j -lSDL2 -lmingw32 -lSDL2main $(optimize)
	#for static linking:
	#g++ sdl.cpp -ob -std=c++20 -IC:/Stuff/vsync/j -lm -ldinput8 -ldxguid -ldxerr8 -luser32 -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lshell32 -lsetupapi -lversion -luuid -static -lSDL2 -lmingw32 -lSDL2main $(optimize)
	#I got flags from the SDL folder's pkgconfig/sdl2.pc. both Libs and Libs.private are necessary
	#1.47 MB for doing absolutely nothing: SDL_Init() and SDL_Quit(), doesn't even open a window besides the console. whereas my full glfw program is 507 KB
	#to recompile: https://www.libsdl.org/hg.php
	##define SDL2_LEAN_AND_MEAN in src/SDL_internal.h, but you have to recompile.
	#./configure; make; make install
	#even worse (maybe because I used the trunk). 1.54 MB.

profile:
	g++ unity.cpp $(windowsflags) -g $(optimizeprofiler)

cleanlineendings:
	perl -i -p -e 's/\r//' *
#CR LF to LF https://stackoverflow.com/a/7533652


#iwyu: just replace g++ with include-what-you-use. unfortunately, clang 14 still has a ranges bug which prevents it from compiling, see https://stackoverflow.com/a/65089413 a few years!
#https://github.com/llvm/llvm-project/issues/44178

#meson has unity builds. would speed up intellisense. and single_def still works. but globbing is warned against
#well, I have to add new files somewhere anyway.

#vs code doesn't like spaces in filenames either. clicking on line number links from gcc fails. and the debugger also fails to find files